<?php
namespace app\spadmin\controller\user;
use app\common\controller\Backend;
use think\Db;
use think\Exception;
class User extends Backend{

    private $userLevel;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');
    }
    public function index(){

        $this->request->filter(['strip_tags', 'trim']);
        if($this->request->isAjax()){
            $data = $this->request->param();
            if (isset($data['keywords']) && $data['keywords']) {
                $map['mobile|nickname'] = ['like','%'.$data['keywords'].'%'];
            }           
            $map['status'] = 1;         
            $list =   $this->model->where($map)->order('id desc')->paginate($data['limit'])->toArray();
           
            foreach ($list['data'] as $key => &$value) {
                $value['balance'] = Db::name('Account')->where('id',$value['account_id'])->value('balance');
            }
            return $this->result($list);
        }
        return $this->view->fetch();
    }
    public function detail($ids=null){
        $row = $this->model->get($ids);
        $this->assign('info',$row);
        return $this->view->fetch();
    }
    public function edit($ids=null){
        $row = $this->model->get($ids);
        if($this->request->isPost()){
            $data = $this->request->post('',[],'strip_tags');
            $user = $this->model->where('id',$data['id'])->find();
            
            Db::startTrans();
            try {
                    $user['mobile']=$data['mobile'];
                    $user['score']=$data['score'];
                    $user->save();
                Db::commit();
                $this->success('操作成功');
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
        }else{
            $this->assign('info',$row);
            return $this->view->fetch();
        }
    }
}